package ihm.manes_ho.agenda.data;

import ihm.manes_ho.agenda.vo.MeetingItemVO;
import ihm.manes_ho.agenda.vo.MeetingVO;

import java.util.ArrayList;
import java.util.List;

/**
 * This class represents the database which contains set of rendez-vous informations of the user 
 */
public class HardCodedDB {
	private static final int columns = 7;
	private static final int rows = 6;
	private List<MeetingItemVO> meetingItems = new ArrayList<MeetingItemVO>();
	
	private static HardCodedDB instance;
	
	private HardCodedDB() {
		initMeetingDB();
	}
	
	/**
	 * init hard-coded database
	 */
	private void initMeetingDB() {
		for (int i = 0; i < columns * rows; i++) { //<-> 7 columns x 6 rows = 42 items data..
			meetingItems.add(new MeetingItemVO());
		}
		//add manually some meetings..
		addMeeting("RV 1", "Address1", 0, 8 * 60, 9 * 60);
		addMeeting("RV 2", "Address2", 0, 18 * 60, 19 * 60);		
		addMeeting("RV 3", "Address3", 1, 15 * 60, 16 * 60);		
		addMeeting("RV 4", "Address4", 2, 10 * 60, 11 * 60);		
		addMeeting("RV 5", "Address5", 3, 8 * 60, 9 * 60);
		addMeeting("RV 6", "Address6", 4, 14 * 60, 15 * 60);		
		addMeeting("RV 7", "Address7", 5, 17 * 60, 18 * 60);
		addMeeting("RV 8", "Address7", 6, 8 * 60, 9 * 60);
	}
	
	public static synchronized HardCodedDB getInstance() {
		if (instance == null) {
			instance = new HardCodedDB();
		}
		return instance;
	}
	
	public List<MeetingItemVO> getMeetingData() {
		return meetingItems;
	}
	
	/**
	 * @param column_index_from
	 * @param column_numbers
	 * @return a list of meetings information corresponding to the list of current rendez-vous item views
	 */
	public List<MeetingItemVO> getMeetingItemList(int column_index_from, int column_numbers) {
		List<MeetingItemVO> list = new ArrayList<MeetingItemVO>();
		
		if (column_index_from <= columns - column_numbers) {
			for (int i = 0; i < rows; i++) { //6 rows
				for (int j = 0; j < column_numbers; j++) {
					list.add(meetingItems.get(i * columns /*7 columns*/ + j + column_index_from));
				}
			}
		}
		return list;
	}
	
	/**
	 * add a rendez-vous object/entity into the hard-coded database
	 * @param name
	 * @param address
	 * @param weekday_index
	 * @param timeStart
	 * @param timeEnd
	 */
	public void addMeeting(String name, String address, int weekday_index, int timeStart, int timeEnd) {
		int meetingItemIndex = getMeetingItemIndex(weekday_index, timeStart);
		meetingItems.get(meetingItemIndex).addMeeting(new MeetingVO(name, address, weekday_index, timeStart, timeEnd));
	}
	
	/**
	 * remove rendez-vous (entities) from the database
	 */
	public void removeAllMeetings() {
		for (MeetingItemVO meetingItem : meetingItems) {
			meetingItem.removeAll();
		}
	}
	
	private int getMeetingItemIndex(int weekday_index, int timeStart) {
		int time_index = 0;
		if (timeStart >= 0 && timeStart < 10 * 60)  	 // time < 10h
			time_index = 0;
		else if (timeStart >= 10 && timeStart < 12 * 60) // 10h <= time < 12h
			time_index = 1;
		else if (timeStart >= 12 && timeStart < 14 * 60) // 12h <= time < 14h
			time_index = 2;
		else if (timeStart >= 14 && timeStart < 16 * 60) // 14h <= time < 16h
			time_index = 3;
		else if (timeStart >= 16 && timeStart < 18 * 60) // 16h <= time < 18h
			time_index = 4;
		else				  							 // time => 18h
			time_index = 5;		
		
		return (time_index * columns + weekday_index);
	}
}
